﻿using System.Collections.Generic;
using System.Web.Mvc;

namespace Xwltz.Framework.Mvc
{
    public class BaseFilterAttribute : ActionFilterAttribute
    {
        private readonly List<string> _filterList;
        private readonly string _redirectUrl;

        public BaseFilterAttribute(List<string> filterList, string redirectUrl)
        {
            _filterList = filterList;
            _redirectUrl = redirectUrl;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var controllerName = filterContext.RouteData.Values["controller"].ToString();
            var actionName = filterContext.RouteData.Values["action"].ToString();
            var url = "~/" + controllerName + "/" + actionName;
            if (_filterList.Contains(url))
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {
                var obj = OperatorProvider<object>.Provider.GetCurrent();
                if (obj == null)
                {
                    filterContext.Result = new RedirectResult(_redirectUrl);
                }
                base.OnActionExecuting(filterContext);
            }
        }
    }
}
